function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}


function get_good_list_by_keyword() {
    const products = Array.from(document.getElementsByClassName('product'))
    let resultArray = [
        ['商品名称', '月成交', '评论数量', '价格', '店铺名称', '商品链接', '商品图片', '下载时间']
    ];
    for (var i = 0; i < products.length; i++) {
        const title = products[i].querySelector('.productTitle > a').textContent.trim()
        const sold_ele = products[i].querySelector('p.productStatus > span > em')
        let sold = null;
        if (sold_ele == null) {
            sold = null
        } else {
            sold = sold_ele.textContent.trim()
        }
        const commentsNum = products[i].querySelector('p.productStatus > span:nth-child(2) > a').text.trim();
        const priceText = products[i].querySelector('.productPrice > em').title.trim();
        const shopName = products[i].querySelector('.productShop-name').text.trim();
        const href = products[i].querySelector('.productTitle > a').href.trim();
        var imgPath = products[i].querySelector('a.productImg > img').src.trim();
        if (imgPath.indexOf("http") == -1) {
            imgPath = ''
        }
        const downloadDate = new Date().toLocaleString()
        resultArray.push([title, sold, commentsNum, priceText, shopName, href, imgPath, downloadDate])
    }
    var csv = resultArray.map(function (d) {
        return d.join();
    }).join('\n');
    const fileName = document.getElementsByClassName('s-combobox-input')[0].value.trim() + ".csv";
    const file = new File([csv], fileName, {
        type: "text/csv;charset=utf-8",
    });
    return file;
}

function get_good_list_by_shop() {
    const products = Array.from(document.getElementsByClassName('item'));
    let resultArray = [
        ['商品名称', '月成交', '评论数量', '价格', '商品链接', '商品图片', '下载时间']
    ];
    for (var i = 0; i < products.length; i++) {
        const title = products[i].querySelector('a.item-name').textContent.trim();
        const sold_ele = products[i].querySelector('span.sale-num');
        let sold = null;
        if (sold_ele == null) {
            sold = null
        } else {
            sold = sold_ele.textContent.trim()
        }
        const commentsNum_ele = products[i].querySelector('dd.rates > div.title a > span');
        let commentsNum = null;
        if (commentsNum_ele == null) {
            commentsNum = null
        } else {
            commentsNum = commentsNum_ele.textContent.trim()
        }
        const priceText = products[i].querySelector('span.c-price').textContent.trim();
        const href = products[i].querySelector('a.item-name').href.trim();
        var imgEle = products[i].querySelector('a.J_TGoldData > img');
        if (imgEle == null) {
            imgPath = ''
        } else {
            imgPath = imgEle.src.trim();
            if (imgPath.indexOf("http") == -1) {
                imgPath = ''
            }
        }
        const downloadDate = new Date().toLocaleString();
        resultArray.push([title, sold, commentsNum, priceText, href, imgPath, downloadDate])
    }
    var csv = resultArray.map(function (d) {
        return d.join();
    }).join('\n');
    const fileName = document.querySelector('a.slogo-shopname > strong').textContent.trim() + ".csv"
    console.log(fileName);
    const file = new File([csv], fileName, {
        type: "text/csv;charset=utf-8",
    });
    return file;
}

// 接收来自后台的消息
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.cmd == 'down_good_list') {
        var file = null;
        if (window.location.host == 'list.tmall.com') {
            file = get_good_list_by_keyword()
        } else {
            file = get_good_list_by_shop()
        }
        const a = document.createElement('a');
        a.href = URL.createObjectURL(file);
        a.download = file.name;
        a.click();
    } else {
        tip(JSON.stringify(request));
        sendResponse('我收到你的消息了：' + JSON.stringify(request));
    }
});